package 剑指offer;

/**
 * @author SunYuHang
 * @date 2023-03-14 19:22
 * @ClassName : 不同路径063  //类名
 */

public class 不同路径063 {
    //确定dp数组   dp[i][j]
    //确定递推公式 if(obs[i][j]==0无障碍)dp[i][j]=dp[i-1][j]+dp[i][j-1]
    //初始化  if(obs[i][j]==0无障碍时)dp[i][0]=1 dp[0][j]=1
    //遍历顺序
    //打印dp数组
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        //如果在起点或终点出现了障碍，直接返回0
        if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) {
            return 0;
        }
        for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
            dp[i][0] = 1;
        }
        for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
            dp[0][j] = 1;
        }
        for(int i =1;i<m;i++){
            for(int j =1;j<n;j++){
                dp[i][j]=(obstacleGrid[i][j] == 0) ? dp[i - 1][j] + dp[i][j - 1] : 0;
            }
        }
        return dp[m-1][n-1];
    }
}
